package com.haelth.control;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.JTextArea;

import org.jsoup.Connection;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import com.haelth.comman.basic.BaseQuery;
import com.haelth.models.medicinedata.Medicine;


public class HealthMutilQuery extends BaseQuery<Medicine> implements Runnable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private File obj;
	private String Name;
	private String FindName = "";
	private long wait;
	private long avg;
	private int count;

	public HealthMutilQuery() {
		super();
	}

	public void setResault(JTextArea resault) {
		Resault = resault;
	}

	public String getName() {
		return Name;
	}

	public void setName(String name) {
		Name = name;
	}

	public String getFindName() {
		return FindName;
	}

	public void setFindName(String findName) {
		FindName = findName;
	}

	public File getObj() {
		return obj;
	}

	public void setObj(File obj) {
		this.obj = obj;
	}

	public void findbyFile() throws IOException {
		System.out.println("------" + Thread.currentThread().getName()
				+ "-----------");


		FileReader fr = new FileReader(obj.getAbsolutePath());
		BufferedReader br1 = new BufferedReader(fr);
		while ((Name = br1.readLine()) != null) {
			Medicine med = new Medicine();
			med.setId(Name);
			Meds.add(med);
			totalBytes = totalBytes + 1;
		}
		for (Medicine m : Meds) {
			Thread t = new Thread(m);
			t.start();
			try {
				t.join(10);
			} catch (InterruptedException e) {
				// TODO 自動產生的 catch 區塊
				e.printStackTrace();
			}
		}
		if (totalBytes > 4000) {
			wait = 180000;
		} else if (totalBytes > 3000) {
			wait = 150000;
		} else if (totalBytes > 2000) {
			wait = 120000;
		} else if (totalBytes > 1000) {
			wait = 90000;
		} else if (totalBytes > 500) {
			wait = 30000;
		} else if (totalBytes > 250) {
			wait = 15000;
		} else if (totalBytes < 10) {
			wait = 3000;
		} else {
			wait = 10000;
		}
		// System.out.println(wait);
		try {
			Thread.sleep(wait);
		} catch (InterruptedException e) {
			// TODO 自動產生的 catch 區塊
			e.printStackTrace();
		}
		for (Medicine m : Meds) {
			FindName = FindName + m.getcName() + "\n";
		}
		Resault.append(FindName);
		dispose();

	}

	public void run() {
		try {
			findbyFile();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
